package cn.akira.domain.activity.service.quota;

import cn.akira.domain.activity.model.aggregate.CreateQuotaOrderAggregate;
import cn.akira.domain.activity.model.entity.ActivityCountEntity;
import cn.akira.domain.activity.model.entity.ActivityEntity;
import cn.akira.domain.activity.model.entity.ActivitySkuEntity;
import cn.akira.domain.activity.model.entity.SkuRechargeEntity;
import cn.akira.domain.activity.repository.IActivityRepository;
import cn.akira.domain.activity.service.IRaffleActivityAccountQuotaService;
import cn.akira.domain.activity.service.quota.rule.chain.IActionChain;
import cn.akira.domain.activity.service.quota.rule.chain.factory.DefaultActivityChainFactory;
import cn.akira.types.enums.ResponseCode;
import cn.akira.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @author fjl95
 * @date 2025/6/3
 * @description 抽奖活动抽象类，定义标准的流程
 */
@Slf4j
public abstract class AbstractRaffleActivityAccountQuota implements IRaffleActivityAccountQuotaService {

    protected IActivityRepository activityRepository;

    protected DefaultActivityChainFactory defaultActivityChainFactory;

    public AbstractRaffleActivityAccountQuota(IActivityRepository activityRepository, DefaultActivityChainFactory activityChainFactory) {
        this.activityRepository = activityRepository;
        this.defaultActivityChainFactory = activityChainFactory;
    }


    /**
     * 传入sku,创建订单给用户增加抽奖次数
     * @param skuRechargeEntity 活动商品充值实体对象
     * @return
     */
    @Override
    public String createOrder(SkuRechargeEntity skuRechargeEntity) {
        //1.参数校验
        String userId = skuRechargeEntity.getUserId();
        Long sku = skuRechargeEntity.getSku();
        String outBusinessNo = skuRechargeEntity.getOutBusineeeNo();
        if (null == sku || StringUtils.isBlank(userId) || StringUtils.isBlank(outBusinessNo)) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(),ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        //2.查询基础信息
        //2.1查询sku信息
        ActivitySkuEntity activitySkuEntity = activityRepository.queryActivitySku(sku);
        //2.2根据sku查询对应活动信息
        ActivityEntity activityEntity = activityRepository.queryRaffleActivityByActivityId(activitySkuEntity.getActivityId());
        //2.3根据sku查询次数信息
        ActivityCountEntity activityCountEntity = activityRepository.queryRaffleActivityCountByActivityCountId(activitySkuEntity.getActivityCountId());

        //3.活动动作规则校验
        IActionChain actionChain = defaultActivityChainFactory.openActionChain();
        actionChain.action(activitySkuEntity,activityEntity,activityCountEntity);

        //4.构建订单聚合对象 将前面基础信息转化为订单信息和账户信息
        CreateQuotaOrderAggregate createOrderAggregate = buildOrderAggregate(skuRechargeEntity,activitySkuEntity,activityEntity,activityCountEntity);

        //5.保存订单
        doSaveOrder(createOrderAggregate);

        //6.返回单号
        return createOrderAggregate.getActivityOrderEntity().getOrderId();
    }
    protected abstract CreateQuotaOrderAggregate buildOrderAggregate(SkuRechargeEntity skuRechargeEntity, ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity);

    protected abstract void doSaveOrder(CreateQuotaOrderAggregate createOrderAggregate);


}
